package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.bean.PhotoBean;
import com.bocai.yuweiwangluo.bean.UserInfoBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IPersonInfoView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class PersonInfoPresenter
{
    private PhotoBean photoBean;
    private BaseBean baseBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IPersonInfoView personInfoView;
    private Context context;
    private BaseView baseView;
    private int requstType;
    private UserInfoBean userInfoBean;
    public PersonInfoPresenter(Context context, IPersonInfoView personInfoView, BaseView baseView)
    {
        this.context = context;
        this.personInfoView = personInfoView;
        this.baseView=baseView;
    }

    /**
     * 用户上传头像请求
     */
    public void upLoadPhoto(String pic,String ext)
    {
        requstType=1;
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.uploadPhoto(UrlData.BASEURL + UrlData.UPLOADPHOTO, pic, ext, handler);
    }
    public  void updatePhoto()
    {
        requstType=2;
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.updataPhoto(UrlData.BASEURL + UrlData.UPDATAPHOTO, SharePrefencesUtil.getUser_id(context), photoBean.getPath(), handler);
    }
    public  void updataInfo(String sex,String area,String type_id)
    {
        baseView.showLoading();
        requstType=3;
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.updatainfo(UrlData.BASEURL + UrlData.UPDATAINFO, SharePrefencesUtil.getUser_id(context), sex, area,type_id, handler);
    }
    public  void getUserInfo()
    {
            baseView.showLoading();
            requstType=4;
            MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
            myOkHttpClient.getInfo(UrlData.BASEURL + UrlData.GETINFO, SharePrefencesUtil.getUser_id(context), handler);
    }
    public String getPhotoPath(){
        String path = "";
        if (photoBean!=null){
            path=photoBean.getPath();
        }
        return path;
    }

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);
                        if(1==requstType)
                        {

                            photoBean=gson.fromJson(result,PhotoBean.class);
                            if(photoBean.getState()==0)
                            {
                                updatePhoto();
                            }else
                            {
                                baseView.hideLoading();
                                baseView.showError(photoBean.getMsg());
                            }
                        }else if(2==requstType)
                        {

                            baseBean=gson.fromJson(result,BaseBean.class);
                            if(baseBean.getState()==0)
                            {
                                baseView.hideLoading();
                                Toast.makeText(context,"上传成功",Toast.LENGTH_SHORT).show();
                                personInfoView.photoSucceseCallBack();
                                SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor=sharedPreferences.edit();
                                editor.putString("photo",photoBean.getPath());
                                editor.commit();
                            }else
                            {
                                baseView.hideLoading();
                                baseView.showError(baseBean.getMsg());
                            }
                        }else if(3==requstType)
                        {
                            baseView.hideLoading();
                            baseBean=gson.fromJson(result,BaseBean.class);
                            if(baseBean.getState()==0)
                            {
                                Toast.makeText(context,"修改成功",Toast.LENGTH_SHORT).show();

                            }else
                            {
                                baseView.hideLoading();
                                baseView.showError(baseBean.getMsg());
                            }
                        }else if(4==requstType)
                        {

                            baseView.hideLoading();
                            userInfoBean=gson.fromJson(result,UserInfoBean.class);
                            setDatatoView();
                            SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor=sharedPreferences.edit();
                            editor.putString("photo",userInfoBean.getRow().getIcon());
                            editor.commit();
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.hideLoading();
                        baseView.showError(result);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };

    private void setDatatoView() {
        try{
            personInfoView.getAreaTv().setText(userInfoBean.getRow().getArea());
            personInfoView.getNickNameTv().setText(userInfoBean.getRow().getNickname());
            personInfoView.getSexTv().setText(userInfoBean.getRow().getSex());
            ImageLoaderUtil.loaderImage(UrlData.IMAGEURL + userInfoBean.getRow().getIcon(), personInfoView.getPhotoView(), R.mipmap.photo_default);
            if(userInfoBean.getRow().getType_id()==null){

                personInfoView.getSportType().setText("请选择擅长运动");
                personInfoView.setTypeId("-1");
            }else{
                personInfoView.getSportType().setText(userInfoBean.getRow().getType_tit().toString());
                personInfoView.setTypeId(userInfoBean.getRow().getType_tit().toString());
            }
        }catch (Exception e){}

    }
}